/*
 * Copyright (C) 2009 Rafael Fernandes
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.phonebooksharing.android.services;

import java.util.Calendar;
import java.util.GregorianCalendar;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.IBinder;
import android.os.SystemClock;

import com.phonebooksharing.android.async.MediaStoreImageScannerTask;
import com.phonebooksharing.android.async.MediaStoreVideoScannerTask;
import com.phonebooksharing.android.util.Global;
import com.phonebooksharing.android.util.NetworkHandlerTask;

public class NotSharingUpdaterService extends Service {
    // Check interval: every 24 hours
    private static long UPDATES_CHECK_INTERVAL = 8 * 60 * 60 * 1000;
    
    public static void schedule(Context context) {
        final Intent intent = new Intent(context, NotSharingUpdaterService.class);
        final PendingIntent pending = PendingIntent.getService(context, 0, intent, 0);
        
        Calendar c = new GregorianCalendar();
        c.add(Calendar.DAY_OF_YEAR, 1);
        c.set(Calendar.HOUR_OF_DAY, 0);
        c.set(Calendar.MINUTE, 0);
        c.set(Calendar.SECOND, 0);
        c.set(Calendar.MILLISECOND, 0);

        final AlarmManager alarm = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
        alarm.cancel(pending);
//        if (DEBUG) {
            alarm.setRepeating(AlarmManager.ELAPSED_REALTIME, SystemClock.elapsedRealtime(),
                    UPDATES_CHECK_INTERVAL, pending);
//        } else {
//            alarm.setRepeating(AlarmManager.RTC, c.getTimeInMillis(),
//                    UPDATES_CHECK_INTERVAL, pending);
//        }
    }
    
    private ImageMediaScannerTask task;
    private VideoMediaScannerTask task2;
    
    @Override
    public void onStart(Intent intent, int startId) {
        super.onStart(intent, startId);
        (task = new ImageMediaScannerTask()).execute(this);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (task != null && task.getStatus() == NetworkHandlerTask.Status.RUNNING) {
            task.cancel(true);
        }
        
        if (task2 != null && task2.getStatus() == NetworkHandlerTask.Status.RUNNING) {
            task2.cancel(true);
        }
    }

    public IBinder onBind(Intent intent) {
        return null;
    }
    
    class ImageMediaScannerTask extends MediaStoreImageScannerTask {

        public ImageMediaScannerTask() {
            Global.loadConfig(getBaseContext());
        }

        @Override
        public void onPostExecute(Void void1) {
            super.onPostExecute(void1);
            (task2 = new VideoMediaScannerTask()).execute(NotSharingUpdaterService.this);
        }
    }

    class VideoMediaScannerTask extends MediaStoreVideoScannerTask {

        public VideoMediaScannerTask() {
            Global.loadConfig(getBaseContext());
        }

        @Override
        public void onPostExecute(Void void1) {
            super.onPostExecute(void1);
            stopSelf();
        }
    }
}